#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "Person.h"
#include "Teacher_Func.h"
#include "Student_Func.h"
#include "Common_Func.h"

//有空节点的链表
//头节点不存放数据
pTeacher_t Teacher_Init() {

    pTeacher_t phead = (pTeacher_t)malloc(sizeof(Teacher_t));
    if (phead == NULL)
    {
        perror("malloc Teacher fail");
        return phead;
    }
    memset(phead,0,sizeof(Teacher_t));
    phead->next = phead;
    phead->prev = phead;
    return phead;
}

//创建节点
pTeacher_t Create_Teacher_node(char *account,char *password,char *name,int age,char sex){

    pTeacher_t pnew=(pTeacher_t)malloc(sizeof(Teacher_t));
    if(pnew==NULL)
    {
        perror("malloc a new node fail");
        return pnew;
    }
    strcpy(pnew->account,account);
    strcpy(pnew->password,password);
    strcpy(pnew->name,name);
    pnew->sex=sex;
    pnew->age=age;
    pnew->status=EMPLOYED;
    pnew->next=NULL;
    pnew->prev=NULL;
    return pnew;
}

//从键盘创建学生节点(学生只有创建账号密码和个人信息的权限,其它都初始化为0)
pTeacher_t Create_new_Teacher_scanf(void){

    pTeacher_t pnew=(pTeacher_t)malloc(sizeof(Teacher_t));
    if(pnew==NULL)
    {
        perror("malloc a new node fail");
        return pnew;
    }
    //已经将所有成员初始化为0
    memset(pnew,0,sizeof(Teacher_t));
    char account[50];
    char password[50];
    char name[20];
    int age;
    char sex;
    printf("请输入账号：");
    scanf("%s",account);
    while(getchar()!='\n');
    printf("请输入密码：");
    scanf("%s",password);
    while(getchar()!='\n');
    printf("请输入姓名：");
    scanf("%s",name);
    while(getchar()!='\n');
    printf("请输入年龄：");
    scanf("%d",&age);
    while(getchar()!='\n');
    printf("请输入性别: ");
    scanf("%c",&sex);
    while(getchar()!='\n');
    strcpy(pnew->account,account);
    strcpy(pnew->password,password);
    strcpy(pnew->name,name);
    pnew->sex=sex;
    pnew->age=age;
    pnew->next=NULL;
    pnew->prev=NULL;
    return pnew;
}


//头插法
void  Teacher_Head_Insert(pTeacher_t phead, pTeacher_t node)
{   
   if(node == NULL)
   {
    perror("Head insert a node fail");
    return;
   }
   pTeacher_t plink=phead;
   //判断是否首节点
   if (phead->next == phead&&phead->prev==phead)
    {
        plink->prev = node;
        plink->next = node;
        node->prev = phead;
        node->next = phead;
        printf("注册成功\n");
        printf("个人信息如下:\n");
        Print_One_Teacher(node);
        return;
    }
   //头插
    node->next = phead->next;
    node->prev = phead;
    phead->next->prev=node;
    phead->next = node;
    printf("注册成功\n");
    printf("个人信息如下:\n");
    Print_One_Teacher(node);
}

void Print_Teacher_List(pTeacher_t phead)
{
    pTeacher_t plink=phead->next;

    if(plink==phead)
    {
        perror("None Link List");
        return;
    }
    char *status[]={"在职","停职","离职"};
    while(plink!=phead)
    {
        printf("账号:%s,姓名:%s,性别:%c,年龄:%d,状态:%s\n",plink->account,plink->name,plink->sex,plink->age,status[plink->status]);
        plink=plink->next;
    }
    printf("\n");

}

void Print_One_Teacher(pTeacher_t plink)
{
    char *status[]={"在职","停职","离职"};
    printf("账号:%s,姓名:%s,性别:%c,年龄:%d,状态:%s\n",plink->account,plink->name,plink->sex,plink->age,status[plink->status]);
}

void Release_Teacher_List(pTeacher_t phead)
{
    pTeacher_t plink = phead->next;
    pTeacher_t ptemp = NULL;

   //不只有头结点的情况
    while (plink != phead)
    {
        ptemp = plink->next;
        free(plink);
        plink = ptemp;
    }
    free(phead);
    
    printf("链表已经全部释放\n");
}

//老师给学生录分
void Teacher_Modify_Student(pStudent_t phead)
{
    char account[50]; 
    int choice,score;
    if (phead->next == phead)
    {
        printf("无学生信息");
        return;
    }

    printf("请输入需要录分的学生账号:");
    scanf("%s",account);
    while(getchar()!='\n');
 
    pStudent_t plink = phead->next;

    while (plink != phead)
    {
        if (strcmp(plink->account,account)==0)
        {
            printf("选择需要选择的功能: 1.录入成绩 2.放弃修改\n");
            scanf("%d",&choice);
            while(choice!=2)
            {
                switch(choice)
                {
                    
                    case 1:
                        printf("请输入新成绩:");
                        scanf("%d",&score);
                        while(getchar()!='\n');
                        plink->score=score;
                        printf("成绩修改成功\n");
                        break;
                    case 2:
                        return;
                        break;
                    default:
                        printf("输入错误，重新输入\n");
                        break;
                }
                printf("选择需要选择的功能: 1.录入成绩 2.放弃修改\n");
                scanf("%d",&choice);
                while(getchar()!='\n');
            }
            return;
        }
        plink = plink->next;
    }

    if (plink == phead)
    {
        printf("未找到该学生账号\n");
        return;
    }


}

//登录验证
pTeacher_t Teacher_Login_check(pTeacher_t phead)
{
    if(phead->next==phead)
    {
        printf("无教师信息");
        return NULL;
    }
    pTeacher_t plink=phead->next;

    char account[20];
    char password[20];
    printf("请输入账号:");
    scanf("%s",account);
    while(getchar()!='\n');
    printf("请输入密码:");
    scanf("%s",password);
    while(getchar()!='\n');
    printf("\n");

    while(plink!=phead)
    {
        if(strcmp(plink->account,account)==0&&strcmp(plink->password,password)==0)
        {
            printf("登录成功\n");
            wait_for_any_key();
            return plink;
        }
        plink=plink->next;
    }
    printf("登录失败\n");
    wait_for_any_key();
    return NULL;

}
